<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">
<head>
    <th:block th:include="include :: header('首页横幅')"/>
    <th:block th:include="include :: bootstrap-fileinput-css" />

    <style>
        .banner-view {
            width: 90%;
            margin: 20px auto;
            height: 200px;
            display: none;
            text-align: center;
        }
        .banner-view img {
            max-height: 100%;
            max-width: 100%;
        }
        .banner-view-close {
            position: absolute;
            top: 10px;
            right: 10px;
            cursor: pointer;
        }
    </style>
</head>
<body class="gray-bg">
<div class="wrapper wrapper-content animated fadeInRight white-bg">

    <div class="banner-view" id="bannerView">
        <img id="bannerImg" src=""/>
        <i id="bannerViewClose" class="banner-view-close fa fa-close"></i>
    </div>

    <form class="form-horizontal m white-bg" id="form-index-banner">
        <div class="form-group" id="fileWrap">
            <label class="col-sm-3 control-label ">图片</label>
            <input type="hidden" name="imgUrl" id="imgUrl" th:value="${imgUrl}"/>
            <div class="col-sm-8">
                <input id="singleFile" name="file" type="file">
            </div>
        </div>
        <div class="form-group">
            <div class="col-sm-8 col-sm-offset-3">
                <button class="btn btn-primary" type="button" onclick="save()">保存</button>
            </div>
        </div>
    </form>
</div>
<th:block th:include="include :: footer"/>
<th:block th:include="include :: bootstrap-fileinput-js" />
</body>

<script th:inline="javascript">
    var prefix = ctx + "info/info"
    var imgUrl =  [[${imgUrl}]]

    if(imgUrl) {
        $('#bannerView').show()
        $('#bannerImg').attr('src', base + imgUrl)
        $('#fileWrap').hide()
    }

    $('#bannerViewClose').on('click', function() {
        $('#bannerView').hide()
        $('#fileWrap').show()
    })

    $("#singleFile").fileinput({
        uploadUrl: ctx + 'common/upload',
        maxFileCount: 1,
        autoReplace: true,
        initialPreviewAsData: true
    }).on('fileuploaded', function (event, data, previewId, index) {
        var rsp = data.response;
        log.info("return url：" + rsp.url)
        log.info("reutrn fileName：" + rsp.fileName)
        log.info("reutrn newFileName：" + rsp.newFileName)
        log.info("return originalFilename：" + rsp.originalFilename)

        $('#imgUrl').val(rsp.fileName)
    }).on('fileremoved', function (event, id, index) {
        $('#imgUrl').val('')
        $("input[name='" + event.currentTarget.id + "']").val('')
    }).on('filedeleted', function() {
        $('#imgUrl').val('')
    }).on('filesuccessremove', function() {
        $('#imgUrl').val('')
    }).on('filecleared', function() {
        $('#imgUrl').val('')
    })

    function save() {
        var imgUrl = $('#imgUrl').val()
        const data = {
            imgUrl: imgUrl
        }
        $.modal.loading("保存中...");
        $.post(ctx + 'info/info/saveIndexBanner', data, function(res) {
            $.modal.closeLoading();
            window.location.reload()
        })
    }
</script>

</html>